package com.inspectortime.domain.type;

import java.util.regex.Pattern;

public enum Country {

    USA(Pattern.compile("^[0-9]{5}$"), "840"),
        CANADA(Pattern.compile("^([a-zA-Z][0-9]){3}$"), "124"),
        PUERTO_RICO(Pattern.compile("^[0-9]{5}$"), "630");

    private final Pattern zipCodePattern;
    private final String isoCode;

    private Country(Pattern pattern, String isoCode) {
        this.zipCodePattern = pattern;
        this.isoCode = isoCode;
    }

    public boolean isZipValid(String zipCode) {
        return zipCodePattern.matcher(zipCode).matches();
    }

    public String getIsoCode() {
        return isoCode;
    }

    public static Country getCountryWithIsoCode(String isoCode) {
        for (Country country : Country.values()) {
            if (country.getIsoCode().equals(isoCode)) {
                return country;
            }
        }
        return null;
    }
}
